import { createRouter, createWebHistory } from 'vue-router'
import login from './modules/login'
import layout from './modules/layout'
import { useUserStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import userPatient from './modules/userPatient'
import consultFast from './modules/consultFast'
import consultDep from './modules/consultDep'
import consultIllness from './modules/consultIllness'
import consultPay from './modules/consultPay'
import room from './modules/room'
import userConsult from './modules/userConsult'
import consultDetail from './modules/consultDetail'
import orderPay from './modules/orderPay'
import orderPayResult from './modules/orderPayResult'
import orderDetail from './modules/orderDetail'
import orderLogistics from './modules/orderLogistics'
import loginCallback from './modules/loginCallback'
// -- append import here --
NProgress.configure({
  showSpinner: false
})

const routes = [
  login,
  layout,
  userPatient,
  consultFast,
  consultDep,
  consultIllness,
  consultPay,
  room,
  userConsult,
  consultDetail,
  orderPay,
  orderPayResult,
  orderDetail,
  orderLogistics,
  // router.ts.hbs
  // -- append router here --
  loginCallback
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to) => {
  NProgress.start()
  // 用户仓库
  const store = useUserStore()
  // 不需要登录的页面，白名单
  const wihteList = ['/login', '/register', '/login/callback']
  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
})
router.afterEach((to) => {
  document.title = `${to.meta.title || ''}-优医问诊`
  NProgress.done()
})

export default router
